package com.be.beadmin.material.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.material.dto.input.PickingReccieveDetailsDto;
import com.be.beadmin.material.dto.output.PickingReccieveDetailsEx;
import com.be.beadmin.material.service.PickingReccieveDetailsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zmy
* @date 2023-05-14
**/
@RestController
public class PickingReccieveDetailsController {

    @Autowired
    private PickingReccieveDetailsService pickingReccieveDetailsService;


    @PostMapping(value = "/product/getPickingReccieveDetailsByPage")
    public ResultModel getPickingReccieveDetailsByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(pickingReccieveDetailsService.getPickingReccieveDetailsByPage(pageEntity)) ;
    }

    @PostMapping("/product/addPickingReccieveDetails")
    public ResultModel addPickingReccieveDetails(@RequestBody PickingReccieveDetailsDto pickingReccieveDetailsDto){
        pickingReccieveDetailsService.addPickingReccieveDetails(pickingReccieveDetailsDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delPickingReccieveDetails")
    public ResultModel delPickingReccieveDetails(@RequestBody PickingReccieveDetailsDto pickingReccieveDetailsDto){
        pickingReccieveDetailsService.delPickingReccieveDetails(pickingReccieveDetailsDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updatePickingReccieveDetails")
    public ResultModel updatePickingReccieveDetails(@RequestBody PickingReccieveDetailsDto pickingReccieveDetailsDto){
        pickingReccieveDetailsService.updatePickingReccieveDetails(pickingReccieveDetailsDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportPickingReccieveDetails")
    public void exportPickingReccieveDetails(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PickingReccieveDetailsEx> pickingReccieveDetailsPage = pickingReccieveDetailsService.getPickingReccieveDetailsByPage(pageEntity);
        List<PickingReccieveDetailsEx> pickingReccieveDetailsExes = pickingReccieveDetailsPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PickingReccieveDetails接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PickingReccieveDetailsEx.class).sheet("sheet1").doWrite(pickingReccieveDetailsExes);
    }

    @ApiOperation("根据柜体编码获取物料对比信息")
    @PostMapping("/material/getMaterialComparisonsByProductCode")
    public ResultModel getMaterialComparisonsByProductCode(@RequestParam String productCode){
        return ResultModel.success("success" , pickingReccieveDetailsService.getMaterialComparisonsByProductCode(productCode));
    }
}